<?php


namespace app\admin\controller\sergood;

use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;
use think\facade\Db;

/**
 * @ControllerAnnotation(title="服务产品列表")
 */
class ServiceGoods extends AdminController
{

    use \app\admin\traits\Curd;
    protected $sort = [
        'id'   => 'desc',
    ];

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new \app\admin\model\ServiceGoods();
        
    }
    
    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            list($page, $limit, $where) = $this->buildTableParames();
            $count = $this->model
                ->where($where)
                ->count();
            $list = $this->model
                ->where($where)
                ->page($page, $limit)
                ->order($this->sort)
                ->select();
                foreach ($list as $k => $v){
                    //分类
                    if(!empty($v['service_class'])){
                        $class_name = Db::name('classification')->where('id',$v['service_class'])->field('title')->find();
                    }else{
                        $class_name['title'] = "";
                    }
 
                    $v['service_name'] = $class_name['title'];
                    $list[$k] = $v;
                }
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }
 

    /**
    * @NodeAnotation(title="添加")
    */
    public function add()
    {
        if ($this->request->isAjax()) {
            $post = $this->request->param();
            if(empty($post['title'])){
                $this->error('产品名称不能为空');
            }
            $data = [
                'service_class'=>$post['cla_id'],
                'title'=>$post['title'],
                'zhu_img'=>$post['image'],
                'lun_img'=>$post['logo'],
                'desc'=>$post['desc'],
                'title_desc'=>$post['title_desc'],
                'group_buying'=>$post['group_buying'],
                'create_time'=>time()
            ];
            try {
                Db::name('service_goods')->insert($data);
              
            } catch (\Exception $e) {
                $this->error('保存失败:' . $e->getMessage());
            }
            $this->success('保存成功');
        }else{
            $classfcation = Db::name("classification")->order("id desc")->select();
            $this->assign('classfcation', $classfcation);
            return $this->fetch();
        }
        
    }

     /**
    * @NodeAnotation(title="编辑")
    */
    public function edit()
    {
        if ($this->request->isAjax()) {
            $post = $this->request->param();
            if(empty($post['title'])){
                $this->error('产品名称不能为空');
            }
            $data = [
                'service_class'=>$post['cla_id'],
                'title'=>$post['title'],
                'zhu_img'=>$post['image'],
                'lun_img'=>$post['logo'],
                'desc'=>$post['desc'],
                'title_desc'=>$post['title_desc'],
                'group_buying'=>$post['group_buying']
            ];
            try {
               $this->model->where('id',$post['id'])->update($data);
              
            } catch (\Exception $e) {
                $this->error('保存失败:' . $e->getMessage());
            }
            $this->success('保存成功');
        }else{
            $post = $this->request->param(); 
            $row = $this->model->where('id',$post['id'])->find();
            $schedule = htmlspecialchars_decode($row['desc']);
            $row['desc'] = $schedule;

            $classfcation = Db::name("classification")->order("id desc")->select();
            $this->assign('classfcation', $classfcation);
           
            $this->assign('row', $row);
        }
        return $this->fetch();
    }

 
 
} 